package ldh.im.fxgui.component.message.bo;

import javafx.beans.property.*;
import ldh.im.ImConfig;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.status.ImMessageActionStatus;
import ldh.im.fxbase.data.pojo.status.ImMessageFileStatus;
import ldh.im.fxbase.data.pojo.status.ImMessageStatus;
import ldh.im.fxbase.util.RpcUtil;
import ldh.im.util.DateUtil;
import ldh.im.util.ConfigUtil;
import ldh.rpc.cache.CacheService;

import java.util.Date;

public class WrapImMessage {

    private ImMessage imMessage;
    private BooleanProperty selectProperty = new SimpleBooleanProperty(false);  // 消息栏是否被选中
    private ObjectProperty<WrapImMessageStatus> statusProperty = new SimpleObjectProperty(WrapImMessageStatus.no_send);
    private DoubleProperty progress = new SimpleDoubleProperty(0);  // 文件上传和下载的进度
    private BooleanProperty isProgress = new SimpleBooleanProperty(false); // 文件是否正在下载或上传
    private StringProperty content = new SimpleStringProperty("");

    public static WrapImMessage wrap(ImMessage imMessage) {
        return new WrapImMessage(imMessage);
    }

    private WrapImMessage(ImMessage imMessage) {
        this.imMessage = imMessage;
        WrapImMessageStatus wrapImMessageStatus = toWrapImMessageStatus(imMessage);
        statusPropertyProperty().set(wrapImMessageStatus);
        contentProperty().set(imMessage.getContent());
    }

    public ImMessage getImMessage() {
        return imMessage;
    }

    public void setImMessage(ImMessage imMessage) {
        this.imMessage = imMessage;
    }

    public boolean isSelectProperty() {
        return selectProperty.get();
    }

    public BooleanProperty selectPropertyProperty() {
        return selectProperty;
    }

    public void setSelectProperty(boolean selectProperty) {
        this.selectProperty.set(selectProperty);
    }

    public WrapImMessageStatus getStatusProperty() {
        return statusProperty.get();
    }

    public ObjectProperty<WrapImMessageStatus> statusPropertyProperty() {
        return statusProperty;
    }

    public void setStatusProperty(WrapImMessageStatus statusProperty) {
        this.statusProperty.set(statusProperty);
    }

    public double getProgress() {
        return progress.get();
    }

    public DoubleProperty progressProperty() {
        return progress;
    }

    public void setProgress(double progress) {
        this.progress.set(progress);
    }

    public boolean isProgress() {
        return isProgress.get();
    }

    public BooleanProperty isProgressProperty() {
        return isProgress;
    }

    public void setIsProgress(boolean isProgress) {
        this.isProgress.set(isProgress);
    }

    public String getContent() {
        return content.get();
    }

    public StringProperty contentProperty() {
        return content;
    }

    public void setContent(String content) {
        this.content.set(content);
    }

    public void rebuildMessageStatus(ImMessage message) {
        this.imMessage = message;
        WrapImMessageStatus status = toWrapImMessageStatus(this.getImMessage());
        this.statusPropertyProperty().set(status);
        this.contentProperty().set(message.getContent());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!(obj instanceof WrapImMessage)) return false;
        WrapImMessage wrapImMessage = (WrapImMessage) obj;
        ImMessage imMessage1 = wrapImMessage.getImMessage();
        ImMessage imMessage2 = this.getImMessage();
        return imMessage1.getId().equals(imMessage2.getId());
    }

    @Override
    public int hashCode() {
        return this.getImMessage().getId().intValue();
    }

    private WrapImMessageStatus toWrapImMessageStatus(ImMessage imMessage) {
        Date localDateTime = imMessage.getCreateDateTime();
        int sendTime = ConfigUtil.getInt("message.sendTime", 1);  // 消息发送需要的最大时间，如果这个时间内未完成发送，标志着消息未发送成功
        ImMessageStatus status = imMessage.getStatus();
        ImMessageActionStatus actionStatus = imMessage.getActionStatus();

        if(actionStatus.isWithdraw()) return WrapImMessageStatus.empty;
        if(status.isWithdraw()) return WrapImMessageStatus.empty;
        if (status == ImMessageStatus.failure) return WrapImMessageStatus.no_send;
        if (status == ImMessageStatus.sending) return WrapImMessageStatus.sending;
        if (status == ImMessageStatus.readed) return WrapImMessageStatus.empty;

        if (status == ImMessageStatus.created && !DateUtil.hasExceededTime(localDateTime, sendTime)) {
            return WrapImMessageStatus.no_send;
        } else if (status == ImMessageStatus.created && DateUtil.hasExceededTime(localDateTime, sendTime)) {
            return WrapImMessageStatus.empty;
        } else if (status == ImMessageStatus.sended) { // 发送成功
            WrapImMessageStatus wrapImMessageStatus = null;
            if (imMessage.isAttachFile()) {
                wrapImMessageStatus = toWrapImMessageFileStatus(imMessage);
            }
            if (wrapImMessageStatus == null || wrapImMessageStatus == WrapImMessageStatus.empty) {
                if (actionStatus != ImMessageActionStatus.readed) {
                    return WrapImMessageStatus.no_read;
                } else {
                    return WrapImMessageStatus.empty;
                }
            } else {
                return wrapImMessageStatus;
            }
        } else if (status == ImMessageStatus.ref) { // 发送成功
            return WrapImMessageStatus.no_read;
        }
        throw new RuntimeException("不支持这种类型 " + status.name());
    }

    private WrapImMessageStatus toWrapImMessageFileStatus(ImMessage imMessage) {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        String userId = (String) cacheService.getCache(ImConfig.LOGIN_USER_ID);
        ImMessageFileStatus fileStatus = imMessage.getFileStatus();
        Date localDateTime = imMessage.getCreateDateTime();
        int sendTime = ConfigUtil.getInt("message.fileSendTime", 5);
        this.isProgress.set(false);
        if (fileStatus == null) {
            return WrapImMessageStatus.no_upload;
        }
        if (fileStatus == ImMessageFileStatus.created && !DateUtil.hasExceededTime(localDateTime, sendTime)) {
            return WrapImMessageStatus.no_upload;
        } else if (fileStatus == ImMessageFileStatus.uploaded) {
            return WrapImMessageStatus.empty;
        } else if (fileStatus == ImMessageFileStatus.uploading) {
            this.isProgress.set(true);
            return WrapImMessageStatus.uploading;
        } else if (fileStatus == ImMessageFileStatus.downloaded) {
            return WrapImMessageStatus.empty;
        } else if (fileStatus == ImMessageFileStatus.none && !imMessage.isSender(userId)) {
            return WrapImMessageStatus.no_download;
        } else if (fileStatus == ImMessageFileStatus.downloaded) {
            return WrapImMessageStatus.empty;
        } else if (fileStatus == ImMessageFileStatus.downloading) {
            this.isProgress.set(true);
            return WrapImMessageStatus.downloading;
        } else if (fileStatus == ImMessageFileStatus.created) {
            return WrapImMessageStatus.no_upload;
        }  else if (fileStatus == ImMessageFileStatus.none) {
            return WrapImMessageStatus.empty;
        }
        throw new RuntimeException("不支持这种类型2 " + fileStatus.name());
    }
}
